package com.jdy.haoduoaiteacher.view.calendar.fragment.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.Interpolator;
import android.widget.Scroller;

import com.jdy.haoduoaiteacher.view.calendar.abs.RefreshListener;

import java.lang.reflect.Field;

/**
 * Created by Administrator on 2016/6/13 0013.
 */
public class HasTwoAdapterViewpager extends ViewPager {
  public HasTwoAdapterViewpager(Context context) {
    super(context);
    // setViewPagerScrollSpeed( );
  }

  public HasTwoAdapterViewpager(Context context, AttributeSet attrs) {
    super(context, attrs);
    // setViewPagerScrollSpeed( );
  }

  private RefreshListener listener;

  public void setListener(RefreshListener listener) {
    this.listener = listener;
  }

  @Override
  public void setVisibility(int visibility) {
    super.setVisibility(visibility);
    // 刷新，滚动到相应的位置
    if (visibility == VISIBLE) {// 在viewpager显示前，刷新
      if (listener != null) {
        listener.refreshListener(HasTwoAdapterViewpager.this);
      }
    }
  }

  /**
   * 设置ViewPager的滑动速度
   * 
   * */
  private void setViewPagerScrollSpeed() {
    try {
      Field mScroller = null;
      mScroller = ViewPager.class.getDeclaredField("mScroller");
      mScroller.setAccessible(true);
      FixedSpeedScroller scroller = new FixedSpeedScroller(
          HasTwoAdapterViewpager.this.getContext());
      mScroller.set(HasTwoAdapterViewpager.this, scroller);
    } catch (NoSuchFieldException e) {

    } catch (IllegalArgumentException e) {

    } catch (IllegalAccessException e) {

    }
  }

  /**
   * 上一次x坐标
   */
  private float beforeX;

  private boolean isCanScroll = true;

  // -----禁止左滑-------左滑：上一次坐标 > 当前坐标
  @Override
  public boolean dispatchTouchEvent(MotionEvent ev) {
    if (isCanScroll) {
      return super.dispatchTouchEvent(ev);
    } else {
      switch (ev.getAction()) {
      case MotionEvent.ACTION_DOWN:// 按下如果‘仅’作为‘上次坐标’，不妥，因为可能存在左滑，motionValue大于0的情况（来回滑，只要停止坐标在按下坐标的右边，左滑仍然能滑过去）
        beforeX = ev.getX();
        break;
      case MotionEvent.ACTION_MOVE:
        float motionValue = ev.getX() - beforeX;
        if (motionValue > 0) {// 禁止左滑
          return true;
        }
        beforeX = ev.getX();// 手指移动时，再把当前的坐标作为下一次的‘上次坐标’，解决上述问题

        break;
      default:
        break;
      }
      return super.dispatchTouchEvent(ev);
    }

  }

  public boolean isScrollble() {
    return isCanScroll;
  }

  /**
   * 设置 是否可以滑动
   * 
   * @param isCanScroll
   */
  public void setScrollble(boolean isCanScroll) {
    this.isCanScroll = isCanScroll;
  }

  public class FixedSpeedScroller extends Scroller {
    private int mDuration = 0;

    public FixedSpeedScroller(Context context) {
      super(context);
    }

    public FixedSpeedScroller(Context context, Interpolator interpolator) {
      super(context, interpolator);
    }

    public FixedSpeedScroller(Context context, Interpolator interpolator,
        boolean flywheel) {
      super(context, interpolator, flywheel);
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy, int duration) {
      super.startScroll(startX, startY, dx, dy, mDuration);
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy) {
      super.startScroll(startX, startY, dx, dy, mDuration);
    }
  }
}
